﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
  public  class EntryBillManager
    {
        private DataTable CreateTable(List<EntryBill> list)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("EntryBillID", Type.GetType("System.Int32"));
            tb.Columns.Add("EmployeeID", Type.GetType("System.String"));
            tb.Columns.Add("Date", Type.GetType("System.DateTime"));
            tb.Columns.Add("SupplierID", Type.GetType("System.String"));
            tb.Columns.Add("Status", Type.GetType("System.String"));
            foreach (EntryBill a in list)
            {
                DataRow newrow = tb.NewRow();
                object[] array = new object[5];
                array[0] = a.EntryBillID;
                array[1] = a.EmployeeID;
                array[2] = a.Date;
                array[3] = a.SupplierID;
                array[4] = a.Status;
                newrow.ItemArray = array;
                tb.Rows.Add(newrow);
            }
            return tb;
        }
        public DataTable GetAll()
        {
            List<EntryBill> EntryBillList = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                EntryBillList = (from a in ctx.EntryBills where a.Status.CompareTo("Đang tồn tại") == 0 select a).ToList<EntryBill>();
            }
            return CreateTable(EntryBillList);
        }
        public DataTable SearchID(int ID)
        {
            List<EntryBill> entrybills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                entrybills = ctx.EntryBills.Where(a => a.EntryBillID == ID).ToList<EntryBill>();
            }
            if (entrybills.Count!=0)
                return CreateTable(entrybills);
            return null;
        }
        public DataTable SearchByDay(DateTime day)
        {
            List<EntryBill> entrybills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                entrybills = ctx.EntryBills.Where(a => a.Date.Day == day.Day && a.Date.Month == day.Month && a.Date.Year == day.Year).ToList<EntryBill>();
            }
            if (entrybills.Count != 0)
             return CreateTable(entrybills);
            return null;
        }
        public DataTable SearchBySupplierID(string supID)
        {
            List<EntryBill> list = null;
            using (BookStoreEntities contex = new BookStoreEntities())
            {
                list = contex.EntryBills.Where(a => a.SupplierID.CompareTo(supID) == 0).ToList<EntryBill>();
            }
            if (list.Count != 0) 
             return CreateTable(list);
             return null;
        }
        public int Insert(EntryBill b)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                ctx.EntryBills.Add(b);
                ctx.SaveChanges();
                SystemLogManager.Insert("Thêm phiếu nhập: " + b);
                return b.EntryBillID;
            }
        }
        public String Delete(EntryBill entrybill)
        {
            EntryBill b = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                b = ctx.EntryBills.Where(a => a.EntryBillID == entrybill.EntryBillID).FirstOrDefault<EntryBill>();
                if (b != null)
                {
                    b.Status = entrybill.Status;
                    ctx.SaveChanges();
                    SystemLogManager.Insert("Xóa phiếu nhập: " + entrybill);
                    return "Xóa thành công";
                }
                else
                    return "Hóa đơn không tồn tại";
            }
        }
        public string UpdateStatus(EntryBill t)
        {
            EntryBill entrybill = null;
            string kq = "";
            using (BookStoreEntities ctx = new BookStoreEntities())
            {

                entrybill = ctx.EntryBills.Where(a => a.EntryBillID == t.EntryBillID).FirstOrDefault<EntryBill>();
                try
                {
                    if (entrybill != null)
                    {
                        entrybill.Status = t.Status;
                        entrybill.Date = t.Date;
                        entrybill.SupplierID = t.SupplierID;
                        ctx.SaveChanges();
                        t = entrybill;
                        SystemLogManager.Insert("Cập nhật phiếu nhập: " + t);
                        kq = "Cập nhật thành công";
                    }
                }
                catch
                {
                    kq = "Cập nhật thất bại";
                }
            }
            return kq;
        }
        public string GetNameProduct(string productID)
        {
            Product product = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                product = (from a in ctx.Products where a.ProductID.CompareTo(productID) == 0 select a).First<Product>();

            }
            return product.ProductName;
        }
        public DataTable SearchByDay(DateTime Startday, string Type)
        {

            List<EntryBill> entrybills = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                switch (Type)
                {

                    case "Month":
                        entrybills = ctx.EntryBills.Where(a => a.Date.Month == Startday.Date.Month && a.Status.CompareTo("Đang tồn tại") == 0 && a.Date.Year == Startday.Date.Year).ToList<EntryBill>();
                        break;
                    case "Year":
                        entrybills = ctx.EntryBills.Where(a => a.Date.Year == Startday.Date.Year && a.Status.CompareTo("Đang tồn tại") == 0).ToList<EntryBill>();
                        break;
                }

            }
            if (entrybills.Count != 0)
                return CreateTable(entrybills);
            return null;
        }
    }
}
